<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\SearchExogenousDatum;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

use function PHPSTORM_META\type;

class SearchExogenousDatumController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SearchExogenousDatum(), function (Grid $grid) {
            // $grid->model()->orderby('id', 'desc');
            $grid->column('id')->display(function(){
                return $this->id;
            })->sortable()->width("20%");
            $grid->column('content')->display(function(){
                $data = json_decode($this->content, true);
                $sex = $data['sex'] == 1 ? '男' : '女';
                return "{$data['nikename']} {$sex} {$data['age']} {$data['constellation']} {$data['height']} {$data['location']['city']} {$data['education']} {$data['school_type']} {$data['annual_income']} {$data['marital_status']} {$data['assets']} ...";
            })->limit(300)->width("50%");
            $grid->column('created_at');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
            $grid->export();
            $grid->disableDeleteButton();
            $grid->disableEditButton();
            $grid->disableCreateButton();
            $grid->disableRowSelector();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new SearchExogenousDatum(), function (Show $show) {
            $keys = [
                'uid'=> 'ID', 'uid_visible'=> '标识', 'source_type'=> '类型', 'url'=> '网址', 'source'=> '来源', 'nikename'=> '昵称',
                'avatar'=> '头像', 'sex'=> '性别', 'age'=> '年龄', 'constellation'=> '星座', 'height'=> '身高', 'location'=> '所在地',
                'country'=> '国家', 'province'=> '省', 'city'=> '市', 'district'=> '区', 'hometown'=> '家乡', 'education'=> '学历',
                'school_type'=> '学校标签', 'annual_income'=> '收入', 'marital_status'=> '婚姻情况', 'industry'=> '行业',
                'assets'=> '车房情况', 'hukou'=> '户口', 'tags'=> '标签', 'publish_time'=> '添加时间'
            ];
            $show->field('id');
            $show->contents()->unescape()->as(function($content) use($keys){
                $data = '';
                foreach(json_decode($this->content) as $key=> $value){
                    $key = empty($keys[$key]) ? $key : $keys[$key];
                    $key = '<b>' . $key . '</b>';
                    if(is_string($value) || is_int($value)){
                        $data .= $key . ": " . $value . '<br/>';
                    }else{
                        $temp = $key . ":<br/>";
                        foreach($value as $key2=> $value2){
                            $key2 = empty($keys[$key2]) ? $key2 : $keys[$key2];
                            $key2 = '<b>' . $key2 . '</b>';
                            $temp .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $key2 . ":" . $value2 . "<br/>";
                        }
                        $data .= $temp;
                    }
                }
                return $data;
            });
            $show->panel()
                ->tools(function ($tools) {
                    $tools->disableEdit();
                    $tools->disableDelete();
                });
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SearchExogenousDatum(), function (Form $form) {
            $form->display('id');
            $form->text('content');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
